/************************************************************************
 * RESSOURCESMANAGER class
 * by Marc-Henri Vuillaume
 *************************************************************************
 ** * * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 **********************************************************************/

package mhv.fr.utils;

import java.applet.Applet;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

/**
 * A static ressources manager. 
 * @author Marc-Henri
 */
public class RessourcesManager {
    /**
     * applet linked with the ressources manager. 
     */
    private Applet applet;

    /**
     * Getter method.
     * @return applet
     */
    public Applet getApplet() {
        return applet;
    }

    /**
     * Setter method.
     * @param applet
     */
    public void setApplet(Applet applet) {
        this.applet = applet;
    }
    
    /**
     * Return the path of the filename.
     * @param filename
     * @return
     */
    private URL getURL(String filename) {
        URL url = null;
        try {
            url = applet.getClass().getResource(filename);
        }
        catch (Exception e) { }

        return url;
    }
    
    /**
     * Load the image in the ressource manager.
     * @param filename
     * @return Image
     */
    public Image LoadImage(String filename) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        return tk.getImage(getURL(filename));
    }
    
    /**
     * Create the instance of the ressource manager.
     */
    private static class RessourcesManagerHolder {
        private final static RessourcesManager instance = new RessourcesManager();
    }
    
    /**
     * Return the instance of the ressource manager.
     * @return RessourcesManager
     */
    public static RessourcesManager getInstance() {
    return RessourcesManagerHolder.instance;
    }

}
